/**
 * Project: springyounger.lottery.dal
 * 
 * File Created at 2010-3-19
 * $Id$
 * 
 * Copyright 2009 Zjut Croporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Zjut Edu. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Zjut Edu.
 */
package com.springyounger.lottery.dal.dao.ibatis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.springyounger.lottery.dal.dao.UserDao;
import com.springyounger.lottery.dal.dataobject.User;

/**
 * UsersDaoImpl
 * 
 * @author springyounger
 */
public class UserDaoImpl extends SqlMapClientDaoSupport implements UserDao {

    @SuppressWarnings("unchecked")
    public List<User> getUsers(int pageIndex, int pageSize) {
        Map params = new HashMap();
        pageIndex = pageIndex * pageSize;
        if (pageIndex < 0) {
            pageIndex = 0;
        }
        params.put("pageIndex", pageIndex);
        params.put("pageSize", pageSize);
        return (List<User>) getSqlMapClientTemplate().queryForList("getUsers", params);
    }

    public User getUser(String username) {
        return (User) getSqlMapClientTemplate().queryForObject("getUserByName", username);
    }

    public User getUser(User user) {
        //return null;
        return (User) getSqlMapClientTemplate().queryForObject("getUserByNamePwd", user);
    }

    public int deleteUser(int userid) {
        return getSqlMapClientTemplate().delete("deleteUser", new Integer(userid));
    }

    public User insertUser(User user) {
        getSqlMapClientTemplate().insert("insertUser", user);
        return user;
    }

    public int updateUser(User user) {
        return getSqlMapClientTemplate().update("updateUser", user);
    }

    public int getAllUserCount() {
        return (Integer) getSqlMapClientTemplate().queryForObject("getAllUserCount");
    }

    @SuppressWarnings("unchecked")
    public List<User> getLikeUserByName(int pageIndex, int pageSize, String username) {
        Map params = new HashMap();
        pageIndex = pageIndex * pageSize;
        if (pageIndex < 0) {
            pageIndex = 0;
        }
        params.put("pageIndex", pageIndex);
        params.put("pageSize", pageSize);
        params.put("username", username);
        return (List<User>) getSqlMapClientTemplate().queryForList("getLikeUserByName", params);
    }

    @SuppressWarnings("unchecked")
    public int getLikeUserCount(String username) {
        Map params = new HashMap();
        params.put("username", username);
        return (Integer) getSqlMapClientTemplate().queryForObject("getLikeUserCount", params);
    }

}
